package com.qiaoyanqing.ximalaya.discovermodule.discover_fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_activity.BroadcastTypeActivity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.Broadcast_GridViewAdapter;
import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.Broadcast_RecycleAdapter;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.Broadcast_HttpService;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.Broadcast_Categories;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.Broadcast_Entity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.Broadcast_Radios;
import com.qiaoyanqing.ximalaya.url.Urlconfig;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class Discover_BroadcastFragment extends Fragment implements Callback<Broadcast_Entity>, AdapterView.OnItemClickListener, View.OnClickListener {
    private GridView gridView_little, gridview_more;
    private Broadcast_HttpService service;
    private TextView local_tv, country_tv, province_tv, internet_tv, localradios_btn, localradios_tv;
    private RecyclerView localradios, toplradios;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_broadcast, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        service = new Retrofit.Builder()
                .baseUrl(Urlconfig.BROADCASR)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(Broadcast_HttpService.class);
        service.getData().enqueue(this);

    }

    private void initView(View view) {
        gridView_little = (GridView) view.findViewById(R.id.gridView_littile);
        gridview_more = (GridView) view.findViewById(R.id.gridView_more);
        local_tv = (TextView) view.findViewById(R.id.local_tv);
        country_tv = (TextView) view.findViewById(R.id.country_tv);
        province_tv = (TextView) view.findViewById(R.id.province_tv);
        internet_tv = (TextView) view.findViewById(R.id.internet_tv);
        localradios = (RecyclerView) view.findViewById(R.id.localradios);
        localradios_btn = (TextView) view.findViewById(R.id.localradios_btn);
        localradios_tv = (TextView) view.findViewById(R.id.localradios_tv);
        toplradios = (RecyclerView) view.findViewById(R.id.toplradios);

        local_tv.setOnClickListener(this);
        country_tv.setOnClickListener(this);
        province_tv.setOnClickListener(this);
        internet_tv.setOnClickListener(this);
        gridView_little.setOnItemClickListener(this);
        gridview_more.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (i == 15) {
                    gridview_more.setVisibility(View.GONE);
                    gridView_little.setVisibility(View.VISIBLE);

                }
            }
        });
        gridview_more.setVisibility(View.GONE);
    }

    @Override
    public void onResponse(Call<Broadcast_Entity> call, Response<Broadcast_Entity> response) {
        List<Broadcast_Categories> list = response.body().getData().getCategories();
        list.add(new Broadcast_Categories());
        gridView_little.setAdapter(new Broadcast_GridViewAdapter(list, getContext(), 8));
        gridview_more.setAdapter(new Broadcast_GridViewAdapter(list, getContext(), list.size()));
        List<Broadcast_Radios> localRadiosList = response.body().getData().getLocalRadios();
//        localradios.setAdapter(new Broadcast_RecycleAdapter(getContext(), localRadiosList, localRadiosList.size()));
        toplradios.setAdapter(new Broadcast_RecycleAdapter(getContext(), response.body().getData().getTopRadios(), 3));
    }

    @Override
    public void onFailure(Call<Broadcast_Entity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        if (i == 7) {
            gridview_more.setVisibility(View.VISIBLE);
            gridView_little.setVisibility(View.GONE);

        }
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(getContext(), BroadcastTypeActivity.class);
        switch (view.getId()) {
            case R.id.local_tv:
                intent.putExtra("type", local_tv.getText());
                break;
            case R.id.country_tv:
                intent.putExtra("type", country_tv.getText());
                break;
            case R.id.province_tv:
                intent.putExtra("type", province_tv.getText());
                break;
            case R.id.internet_tv:
                intent.putExtra("type", internet_tv.getText());
                break;
        }
        startActivity(intent);
    }
}
